﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfPractice.MVVM;
using WpfPractice.Model;

namespace WpfPractice.ViewModel
{

    // 定义ViewModel
    public class TestWindowViewModel : ObservableObject
    {
        // 【1】属性简写：保存学生信息的集合
        public ObservableCollection<Student> NamesCollection { get; set; }
        // 【2】属性全写：展示学生信息的集合
        private ObservableCollection<Student> _displayName;
        public ObservableCollection<Student> DisplayName
        {
            get => _displayName;
            set => SetProperty(ref _displayName, value);
        }
        // 【2】属性全写：搜索内容
        private string _searchContent;
        public string SearchContent
        {
            get => _searchContent;
            set => SetProperty(ref _searchContent, value);
        }
        public TestWindowViewModel()
        {
            _searchContent = "";
            _displayName = new ObservableCollection<Student>();
            NamesCollection = new ObservableCollection<Student>();
            // 将NameCollection的信息同步到集合displayName
            //collection的监听事件
            NamesCollection.CollectionChanged += (sender, e) =>
            {
                {
                    switch (e.Action)
                    {
                        case NotifyCollectionChangedAction.Add:
                            var items = (object[])(e.NewItems.SyncRoot);
                            foreach (var item in items)
                            {
                                _displayName.Add(item as Student);
                            }
                            break;
                        case NotifyCollectionChangedAction.Remove:
                            _displayName.RemoveAt(e.OldStartingIndex);
                            break;
                        case NotifyCollectionChangedAction.Reset:
                            _displayName.Clear();
                            break;
                        case NotifyCollectionChangedAction.Move:
                            _displayName.Move(e.OldStartingIndex, e.NewStartingIndex);
                            break;
                        case NotifyCollectionChangedAction.Replace:
                            break;
                    }
                };
            };
            //初始化数据
            NamesCollection.Add(new Student() { Name = "dwayne", Age = 12, Grades = "7年级·3班", });
            NamesCollection.Add(new Student() { Name = "john", Age = 15, Grades = "8年级·5班", });
            NamesCollection.Add(new Student() { Name = "linq", Age = 14, Grades = "8年级·1班", });
            NamesCollection.Add(new Student() { Name = "cool", Age = 11, Grades = "7年级·6班", });
            NamesCollection.Add(new Student() { Name = "team", Age = 16, Grades = "9年级·3班", });
        }
    }


}
